package com.jy.wms.service;

import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.BaseService;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.common.base.SucessException;
import com.jy.wms.dao.WmsPurchaseOrderDetailedMapper;
import com.jy.wms.dao.WmsStockMapper;
import com.jy.wms.pojo.WmsEstimatedArrivalOrder;
import com.jy.wms.pojo.WmsFailedStorage;
import com.jy.wms.dao.WmsFailedStorageMapper;
import com.jy.wms.pojo.WmsShelves;
import com.jy.wms.pojo.WmsStock;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * @ClassName: WmsFailedStorageService.java
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author LH
 * @Date Tue Jan 23 09:31:30 GMT+08:00 2018
 */

@Service
public class WmsFailedStorageService extends BaseService<WmsFailedStorageMapper, WmsFailedStorage> {

    @Autowired
    WmsStockMapper wmsStockMapper;
    /**
     * 修改库存
     *
     * @param list 上架实体集合
     * @return
     */
    @Transactional
    public int confirm(List<WmsShelves> list,String username,String ckid) throws SucessException, ServiceException {
        int a =0;
        int b =0;
        int count =0;
        boolean flag=true;
        for(WmsShelves wmsShelves:list){
            WmsStock wmsStock=wmsStockMapper.selectById(wmsShelves.getKcid());//库存
            //如果待下架数量，待转移数量，冻结数量，破损数量，补货数量其中一个大于0时提示操作错误
            if(Double.parseDouble(wmsStock.getDxjsl())>0||Double.parseDouble(wmsStock.getDzysl())>0||Double.parseDouble(wmsStock.getDjsl())>0||Double.parseDouble(wmsStock.getPssl())>0||Double.parseDouble(wmsStock.getBhsl())>0){
                flag=false;
                break;
            }
        }
        if(flag==true){
            for(WmsShelves wmsShelves:list){
                WmsFailedStorage wmsFailedStorage = new WmsFailedStorage();
                WmsStock wmsStock=wmsStockMapper.selectById(wmsShelves.getKcid());//库存
                Double sl= Double.parseDouble(wmsStock.getSl())-Double.parseDouble(wmsShelves.getSjsl());
                    //修改库存
                    a = dao.updateStock(wmsShelves.getKcid(), sl.toString());
                    wmsFailedStorage.setId(UUIDUtils.uuid());
                    wmsFailedStorage.setAsnId(wmsShelves.getYqdhbdid());
                    wmsFailedStorage.setAsnMxId(wmsShelves.getYqdhbdmxid());
                    wmsFailedStorage.setHpbh(wmsShelves.getShow_bh());
                    wmsFailedStorage.setHpsl(new BigDecimal(wmsShelves.getSjsl()));
                    wmsFailedStorage.setKcid(wmsShelves.getKcid());
                    wmsFailedStorage.setKwid(wmsShelves.getKwid());
                    wmsFailedStorage.setCkid(ckid);
                    wmsFailedStorage.setShid(wmsShelves.getSsshid());
                    wmsFailedStorage.setCzr(username);
                    //增加出库记录
                    b=dao.baseInsert(wmsFailedStorage);
                    count++;

            }
            if (count > 0) {
                throw new SucessException("确认出库成功");
            } else {
                throw new ServiceException("确认出库失败");
            }
        }else{
            throw new ServiceException("确认失败，库存异常，请联系管理员");
        }
    }


    /**
     * 插入一条记录
     *
     * @param entity 实体对象
     * @return int
     */
    public Integer baseInsert(WmsFailedStorage entity) {
        return dao.baseInsert(entity);
    }

    /**
     * 插入一条记录（匹配有值的字段）
     *
     * @param entity 实体对象
     * @return int
     */
    public Integer baseInsertSelective(WmsFailedStorage entity) {
        return dao.baseInsertSelective(entity);
    }

    /**
     * 删除（根据主键ID删除）
     *
     * @param id 主键
     * @return int
     */
    public Integer baseDeleteByPrimaryKey(String id) {
        return dao.baseDeleteByPrimaryKey(id);
    }

    /**
     * 删除（根据ID 批量删除）
     *
     * @param idList 主键ID列表
     * @return int
     */
    public Integer baseDeleteBathPrimaryKeys(List<String> idList) {
        return dao.baseDeleteBathPrimaryKeys(idList);
    }

    /**
     * 分页查询
     *
     * @param entity 实体
     * @return Page<WmsFailedStorage>
     */
    public Page<WmsEstimatedArrivalOrder> baseSelectPage(WmsEstimatedArrivalOrder entity, String hzid, String ssckid, String ckmc, String hzmc) throws ServiceException {
        try {
            PageHelper.startPage(entity.getPage(), entity.getRows());
            return (Page<WmsEstimatedArrivalOrder>) dao.baseSelectPage(entity, hzid, ssckid, ckmc, hzmc);
        } catch (Exception e) {
            throw new ServiceException("查询预期到货通知单列表", e);
        }
    }

    /**
     * 查询（根据主键ID查询）
     *
     * @param id 主键ID
     * @return WmsFailedStorage
     */
    public WmsFailedStorage baseSelectByPrimaryKey(String id) {
        return dao.baseSelectByPrimaryKey(id);
    }

    /**
     * 查询（根据ID 批量查询）
     *
     * @param idList 主键ID列表
     * @return list
     */
    public Integer baseSelectBathPrimaryKeys(List<WmsFailedStorage> idList) {
        return dao.baseSelectBathPrimaryKeys(idList);
    }

    /**
     * 修改 （匹配有值的字段）
     *
     * @param entity 主键ID列表
     * @return int
     */
    public Integer baseUpdateByPrimaryKeySelective(WmsFailedStorage entity) {
        return dao.baseUpdateByPrimaryKeySelective(entity);
    }

    /**
     * 修改（根据主键ID修改）
     *
     * @param entity 主键ID列表
     * @return int
     */
    public Integer baseUpdateByPrimaryKey(WmsFailedStorage entity) {
        return dao.baseUpdateByPrimaryKey(entity);
    }
}
